package com.zzu.chat.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xuexiang.xui.widget.button.ButtonView;
import com.xuexiang.xutil.app.ActivityUtils;
import com.zzu.chat.R;
import com.zzu.chat.bean.BigLabel;
import com.zzu.chat.bean.SmallLabel;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.to_register_text)
    TextView toRegister;    //跳转注册
    @BindView(R.id.login_submit_button)
    ButtonView loginButton; //登录
    @BindView(R.id.login_cancel_button)
    ButtonView cancelButton;    //重置
    @BindView(R.id.login_username)
    EditText username;          //用户名
    @BindView(R.id.login_password)
    EditText password;          //密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.to_register_text, R.id.login_submit_button, R.id.login_cancel_button})
    @Override
    public void onClick(View v) {
        String mobilePhone = "+86"+username.getText().toString();
        String passwordString = password.getText().toString();
        switch (v.getId()) {
            case R.id.to_register_text:
                //注册
                Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.login_submit_button:
                login(mobilePhone, passwordString);    //登录
                break;
            case R.id.login_cancel_button:
                username.setText(null);
                password.setText(null);
                break;
            default:
                break;
        }
    }


    /**
     * 登录
     */
    private void login(String mobilePhone, String passwordString){
        AVUser.loginByMobilePhoneNumber(mobilePhone, passwordString).subscribe(new Observer<AVUser>() {
            public void onSubscribe(Disposable disposable) {}
            public void onNext(AVUser user) {
                // 登录成功
                Log.d("login","登录成功");
                ActivityUtils.startActivity(MainActivity.class);

                List<BigLabel> bigLabels = LitePal.findAll(BigLabel.class);
                if (bigLabels.size()<=0){
                    BigLabel bigLabel = new BigLabel();
                    bigLabel.setName("学习");
                    bigLabel.save();

                    SmallLabel smallLabel = new SmallLabel();
                    smallLabel.setName("阅读");
                    smallLabel.setBigLabel(bigLabel);
                    smallLabel.save();

                    smallLabel = new SmallLabel();
                    smallLabel.setName("数学");
                    smallLabel.setBigLabel(bigLabel);
                    smallLabel.save();

                    smallLabel = new SmallLabel();
                    smallLabel.setName("绘画");
                    smallLabel.setBigLabel(bigLabel);
                    smallLabel.save();

                }
            }
            public void onError(Throwable throwable) {
                // 登录失败（可能是密码错误）
                Toast.makeText(LoginActivity.this, "用户名或密码错误",Toast.LENGTH_SHORT).show();
            }
            public void onComplete() {}
        });
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }
}
